package com.byls.controller;

import com.byls.dao.CaseMapper;
import com.byls.pojo.Lawcase;
import com.byls.repository.CaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/case")
public class CaseController {
    @Autowired
    private CaseRepository caseRepository;
    @Autowired
    private CaseMapper caseMapper;
    @GetMapping("/findall")
    private List<Lawcase> getAll(){
        return caseMapper.getAllCase();
    }
    @GetMapping("/findall/{page}/{size}")
    private Page<Lawcase> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable= PageRequest.of(page-1,size);
        return caseRepository.findAll(pageable);
    }
    @GetMapping("/findbyid/{id}")
    public Lawcase findById(@PathVariable("id") Integer id){
        return caseRepository.findById(id).get();
    }
    @CrossOrigin
    @PostMapping("/save")//tong guo RequestBody jiang jsonduixiang zhuanwei java duixiang
    public String save(@RequestBody Lawcase case1){
        Lawcase res = caseRepository.save(case1);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @PutMapping("/update")
    public String update(@RequestBody Lawcase case1){
        Lawcase res = caseRepository.save(case1);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        caseRepository.deleteById(id);
    }

}
